# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import ValidationError


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    @api.model
    def get_history_price_info(self, line_id=None, product_id=None, name_value=None):
        info = ''
        if not any([line_id, product_id, name_value]):
            return info
        product = None
        if line_id:
            sale_order_line = self.browse(line_id)
            product = sale_order_line.product_id
        elif product_id:
            product = self.env['product.product'].browse(product_id)
        elif name_value:
            product = self.env['product.product'].search([('name', '=', name_value)], limit=1)
        if product:
            info = f'产品：{product.name}  最高价格：{product.nd_top_purchase_price} ' \
                   f'最低价格：{product.nd_low_purchase_price} ' \
                   f'最近价格：{product.nd_recent_purchase_price} ' \
                   f'平均价格：{round(product.nd_avg_purchase_price, 2)}'
        return info
